export default {
  isWEBP () {
    var elem = document.createElement('canvas');
    if ((elem.getContext && elem.getContext('2d'))) {
      return elem.toDataURL('image/webp').indexOf('data:image/webp') === 0;
    } else {
      return false;
    }
  },
  getQuery (_str = window.location.href) {
    _str = _str.split('?');
    _str.splice(0, 1);
    _str = _str.join('');
    let $kv = {};
    let $params = _str.split('&');
    let itemLength = $params.length;
    for (let i = 0; i < itemLength; i++) {
      let item = $params[i];
      let $tmpKV = item.split('=');
      if ($tmpKV.length === 2 && $tmpKV[0].length > 0 && $tmpKV[1].length > 0) {
        $kv[$tmpKV[0]] = $tmpKV[1];
      }
    }
    return $kv;
  },
  getQueryString (_str = window.location.href) {
    _str = _str.split('?');
    _str.splice(0, 1);
    _str = _str.join('');
    return _str;
  },
  hasClass (e, t) {
    return e.className.match(new RegExp('(\\s|^)' + t + '(\\s|$)'));
  },
  addClass (e, t) {
    if (!this.hasClass(e, t)) {
      var n = e.className ? ' ' + t : t;
      e.className += n;
    }
  },
  removeClass (e, t) {
    if (this.hasClass(e, t)) {
      var n = new RegExp('(\\s|^)' + t + '(\\s|$)');
      e.className = e.className.replace(n, '');
    }
  },
  setTitle (_title) {
    document.title = _title;
    var ua = navigator.userAgent.toLowerCase();
    // IPHONE版无法监听TITLE 需要IFRAME触发
    if (ua.indexOf('iphone') > -1) {
      var $body = document.body;
      var $iframe = document.createElement('iframe');
      $iframe.src = '/static/ok.txt';
      $iframe.width = '0px;';
      $iframe.height = '0px;';
      $iframe.onload = function () {
        setTimeout(function () {
          $body.removeChild($iframe);
        }, 0);
      };
      $body.appendChild($iframe);
    }
  }
};
